package com.meng.menggaga.application;

import java.util.HashSet;
import java.util.Set;

import android.app.Application;
import android.os.Handler;
import cn.jpush.android.api.JPushInterface;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.meng.menggaga.protocol.MoccaApi;
import com.meng.menggaga.protocol.MoccaApiImpl;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class MainApplication extends Application {
    private static MainApplication mIns;
    private RequestQueue mRequestQueue;
    private MoccaApi mMoccaApi;

    public static Handler mHandler = new Handler();

    @Override
    public void onCreate() {
        super.onCreate();
        mIns = this;
        mRequestQueue = Volley.newRequestQueue(mIns);
        initImageLoader();
        mMoccaApi = new MoccaApiImpl();
        JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this); // 初始化 JPush

        Set<String> set = new HashSet<String>();
        set.add("");
        JPushInterface.setTags(this, set, null);
    }

    public static MainApplication get() {
        return mIns;
    }

    public MoccaApi getMoccaApi() {
        return mMoccaApi;
    }

    public RequestQueue getRequestQueue() {
        return mRequestQueue;
    }

    private void initImageLoader() {
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().cacheInMemory(true) // 1.8.6包使用时候，括号里面传入参数true
                .cacheOnDisk(true) // 1.8.6包使用时候，括号里面传入参数true
                .build();
        ImageLoaderConfiguration config =
                new ImageLoaderConfiguration.Builder(mIns).threadPriority(Thread.NORM_PRIORITY - 2)
                        .defaultDisplayImageOptions(defaultOptions)
                        .denyCacheImageMultipleSizesInMemory()
                        .tasksProcessingOrder(QueueProcessingType.LIFO) // Not
                        // necessary
                        // in common
                        .build();
        ImageLoader.getInstance().init(config);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

}
